using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Mundo_3D
{
    class Manejador_Modelos : DrawableGameComponent
    {
        List<Modelo_Basico> models = new List<Modelo_Basico>();
        List<Texture2D> texturas = new List<Texture2D>();
        Manejador_Camaras cameramanager;


        public Manejador_Modelos(Game game, Manejador_Camaras cam): base(game)
        {
            cameramanager = cam; 
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }
        
        public override void Update(GameTime gameTime)
        {
            for (int i = 0; i < models.Count; ++i)
            {
                switch (models[i].getTipo())
                {
                    case Tipo_Modelo.modelo:
                        models[i].Update();
                        cameramanager.setPos(models[i].GetWorld().Translation);
                        break;
                }
            }          
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            Camara_Basica cam = cameramanager.getCamara();
            foreach (Modelo_Basico bm in models)
            {
                bm.Draw(cam);
            }
            base.Draw(gameTime);
        }

        public Modelo_Basico getModel(){ 
            foreach (Modelo_Basico bm in models)
            {
                return bm;
            }
            return null;
        }

        public void AddModelos(Modelo_Basico mod)
        {
            models.Add(mod);
        }

        public void AddCamaraSeguimiento(Game game){
            Matrix world = this.getModel().GetWorld();
            Vector3 pos = new Vector3(world.Translation.X, world.Translation.Y+10.0f, world.Translation.Z-25.0f);
            Vector3 target = new Vector3(world.Translation.X, world.Translation.Y, world.Translation.Z);
            Camara_Seguimiento cam = new Camara_Seguimiento(game, pos, target, Vector3.Up);
            cameramanager.addCamera(cam);
        }

        public void AddPlataforma(Vector3 pos, Texture2D text)
        {
            Matrix world = Matrix.CreateWorld(new Vector3(pos.X, pos.Y,pos.Z), Vector3.Forward, Vector3.Up);
            models.Add(new Plataforma(Game.Content.Load<Model>(@"Models\Plataforma"),world, text));
            
        }
    }
}